package threadandrunnable;

public class Threadexam extends Thread{
    public static void main(String[] args) {
        MyThread thread=new MyThread();
        new Thread(thread,"1").start();
        new Thread(thread,"2").start();
    }
    static class MyThread extends Thread {
        private int ticket = 5;
        public void run() {
            synchronized (this) { //实现方法同步
                while (true) {
                    System.out.println(Thread.currentThread().getName() + "卖票---->" + ticket--);
                    if (ticket <= 0) {
                        break;
                    }
                }
            }
        }
    }
}
